package com.yuju.log.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.cache.RedisCache;
import com.yuju.common.Result;
import com.yuju.constant.CacheKey;
import com.yuju.enums.CheckinTypeEnum;
import com.yuju.enums.LogBizTypeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.group.service.IGuestInfoService;
import com.yuju.hotel.enums.CustomersEnum;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.service.IRoomService;
import com.yuju.log.model.LogBizRecordModel;
import com.yuju.log.model.LogBizTypeModel;
import com.yuju.log.service.ILogBizRecordService;
import com.yuju.log.service.ILogBizTypeService;
import com.yuju.log.service.IOrderLogService;
import com.yuju.order.enums.ChannelEnum;
import com.yuju.order.enums.RentTypeEnum;
import com.yuju.order.enums.RoomOrderReturnCodeEnum;
import com.yuju.order.model.ReserveModel;
import com.yuju.order.model.ReserveRoomModel;
import com.yuju.order.model.RoomOrderModel;
import com.yuju.order.service.IReserveRoomService;
import com.yuju.order.service.IReserverService;
import com.yuju.order.service.IRoomOrderGuestService;
import com.yuju.order.service.IRoomOrderService;
import com.yuju.roomStatus.model.RoomTypeStatusModel;
import com.yuju.roomStatus.service.IRoomTypeStatusService;
import com.yuju.util.Format;
import com.yuju.vo.order.ReserveVO;
import com.yuju.vo.order.RoomOrderVO;

/**
 * 
 * 类名：OrderLogServiceImpl  <br />
 *
 * 功能：订单日志<br />
 * 包括：预订、入住、退房、订单异动、离店日志
 * 
 * @author : xuehj <br />
 * @Date : 2017年12月28日 下午2:32:19  <br />
 * @version : 1.1 <br />
 */
@Service
public class OrderLogServiceImpl implements IOrderLogService {
	private RedisCache cache = RedisCache.getInstance();
	@Autowired
	IReserverService reserverService ; 
	@Autowired
	IReserveRoomService reserveRoomService ; 
	@Autowired
	IRoomTypeStatusService roomTypeStatusService ; 
	@Autowired
	IRoomOrderService roomOrderService ; 
	@Autowired
	IRoomService roomService ; 
	@Autowired
	IRoomOrderGuestService roomOrderGuestService ; 
	@Autowired
	IGuestInfoService guestInfoService ; 
	@Autowired
	ILogBizTypeService logBizTypeService ; 
	@Autowired
	ILogBizRecordService logBizRecordService ; 
	
	@Override
	public void before(String path, JSONObject obj) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void log(String path, JSONObject jsonObject, Result re) {
		try {
			System.err.println("===================订单日志服务开始===================");
			System.err.println(path);
			String hotelId = jsonObject.getString("hotelId");
			String groupId = jsonObject.getString("groupId");
			String empId = jsonObject.getString("empId");
			String empName = jsonObject.getString("empName");
			switch (path) {
				/*新增订单*/
				case "/roomOrder/edit":
				{
					roomOrder_edit(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*订单所属*/
				case "/roomOrder/updateForSource":
				{
					guestInfo_updateForSource(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*更改客人信息*/
				case "/guestInfo/update":
				{
					guestInfo_update(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*改价*/
				case "/roomOrder/updateForPrice":
				{
					roomOrder_updateForPrice(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*更改离期*/
				case "/roomOrder/updateForCheckoutTime":
				{
					roomOrder_updateForCheckoutTime(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*换房*/
				case "/roomOrder/updateForRoom":
					{
						roomOrder_updateForRoom(path, jsonObject, re, groupId, hotelId, empId, empName);
						break;
					}
				/*解散联房*/
				case "/roomOrder/quitOrderRela":
				{
					roomOrder_quitOrderRela(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*加入联房*/
				case "/roomOrder/joinOrderRela":
				{
					roomOrder_joinOrderRela(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*解散联防*/
				case "/roomOrder/dissolveOrderRela":
				{
					roomOrder_dissolveOrderRela(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*备注*/
				case "/roomOrder/updateSelectTive":
				{
					roomOrder_updateSelectTive(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				//预定新增
				case "/reserve/edit":
				{
					reserve_edit(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*取消预定*/
				case "/reserve/cancelReserve":
				{
					reserve_cancelReserve(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				/*恢复预定*/
				case "/reserve/recoverReserve":
				{
					reserve_recoverReserve(path, jsonObject, re, groupId, hotelId, empId, empName);
					break;
				}
				default:
					break;
			}
			System.err.println("===================订单日志服务结束===================");
		} catch (Exception e) {
			System.err.println("日志报错了,路径："+path);
			// TODO: handle exception
		}
		
	}
	private void guestInfo_updateForSource(String path, JSONObject jsonObject, Result re, String groupId,
			String hotelId, String empId, String empName) {
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_UPDATE_OWNER_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*参数获取*/
		String id = jsonObject.getString("id");
		BigDecimal roomPrice = jsonObject.getBigDecimal("roomPrice");
		BigDecimal monthPrice = jsonObject.getBigDecimal("monthPrice");
		Byte customType = jsonObject.getByte("customType");
		String checkinStandardName = jsonObject.getString("checkinStandardName");
		String priceSchemeName = jsonObject.getString("priceSchemeName");
		RoomOrderModel roomOrderModel  = (RoomOrderModel) re.getData();
		if(monthPrice != null){
			RoomTypeStatusModel roomTypeStatusModel = roomTypeStatusService.getApartPrice
					(roomOrderModel.getCheckinTime(),roomOrderModel.getCheckoutTime(),monthPrice);
			roomPrice = roomTypeStatusModel.getDayPrice();
		}
		/*模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_UPDATE_OWNER_LOG_BIZ_TYPE.getName());
		templateTY = templateTY.replace("#oldCustomTypeName#", roomOrderModel.getCustomTypeName());
		templateTY = templateTY.replace("#newCustomTypeName#", CustomersEnum.getTypeName(customType));
		templateTY = templateTY.replace("#newRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#oldRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#newRoomPrice#", roomPrice.toString());
		templateTY = templateTY.replace("#activityPrice#", roomPrice.toString());
		templateTY = templateTY.replace("#oldRoomPrice#",roomOrderModel.getRoomPrice().toString());
		templateTY = templateTY.replace("#newPriceSchemeName#", priceSchemeName);
		templateTY = templateTY.replace("#oldPriceSchemeName#", roomOrderModel.getPriceSchemeName());
			/*组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setRemark(roomOrderModel.getRemark());
		logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
		
		/*保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
		
	}
	private void guestInfo_update(String path, JSONObject jsonObject, Result re, String groupId, String hotelId,
			String empId, String empName) {
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_UPDATE_REMARK_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*参数获取*/
		String id = jsonObject.getString("id");
		RoomOrderModel roomOrderModel = roomOrderService.findById(id);
		String remark = jsonObject.getString("remark");
		/*模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_UPDATE_REMARK_LOG_BIZ_TYPE.getName());
		templateTY = templateTY.replace("#roomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#remark#", remark);
			/*组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		/*保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
		
		
		
	}
	private void roomOrder_updateForCheckoutTime(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_OVERSTAY_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*参数获取*/
		String id = jsonObject.getString("id");
		Date checkoutTime = jsonObject.getDate("checkoutTime");
		RoomOrderModel roomOrderModel  = (RoomOrderModel) re.getData();
		/*模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_OVERSTAY_LOG_BIZ_TYPE.getName());
		templateTY = templateTY.replace("#dayCount#" ,String.valueOf(
				Math.round((checkoutTime.getTime()- roomOrderModel.getCheckinTime().getTime())/1000/60/60/24)));
		templateTY = templateTY.replace("#oldCheckoutTime#", Format.getForMatDayForFull(roomOrderModel.getCheckoutTime()));
		templateTY = templateTY.replace("#newCheckoutTime#", Format.getForMatDayForFull(checkoutTime));
		templateTY = templateTY.replace("#newRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#oldRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#oldRoomPrice#", roomOrderModel.getRoomPrice().toString());
		templateTY = templateTY.replace("#newRoomPrice#",roomOrderModel.getRoomPrice().toString());
		templateTY = templateTY.replace("#newPriceSchemeName#",roomOrderModel.getRoomPrice().toString());
		templateTY = templateTY.replace("#oldPriceSchemeName#",roomOrderModel.getRoomPrice().toString());
			/*组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
		logBizRecordModel.setRemark(roomOrderModel.getRemark());
		/*保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
	}
	private void roomOrder_updateForRoom(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_CHANGEROOM_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*3 参数获取*/
		String roomCode = jsonObject.getString("roomCode");
		String oldRoomId = jsonObject.getString("oldRoomId");
		String goReason = jsonObject.getString("goReason");
		String priceSchemeName = jsonObject.getString("priceSchemeName");
		RoomModel roomModel = roomService.findById(oldRoomId);
		BigDecimal roomPrice = jsonObject.getBigDecimal("roomPrice");
		BigDecimal monthPrice = jsonObject.getBigDecimal("monthPrice");
		RoomOrderModel roomOrderModel  = (RoomOrderModel) re.getData();
		/* 4 模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_CHANGEROOM_LOG_BIZ_TYPE.getName())
		.replace("#oldRoomCode#",roomModel.getRoomCode())
		.replace("#newRoomCode#",roomCode)
		.replace("#remark#",goReason)
		.replace("#oldRoomPrice#" ,roomOrderModel.getRoomPrice().toString())
		.replace("#newRoomPrice#", roomPrice.toString());
		templateTY = templateTY.replace("#newPriceSchemeName#", priceSchemeName);
		templateTY = templateTY.replace("#oldPriceSchemeName#", roomOrderModel.getPriceSchemeName());
		if(monthPrice != null){
			templateTY =  templateTY.replace("#newRoomPrice#",getDayPrice(roomOrderModel.getCheckinTime(),
					roomOrderModel.getCheckoutTime(), monthPrice).toString());
		}
			/* 5 组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
		logBizRecordModel.setRemark(goReason);
		/*6 保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
	}
	private void roomOrder_quitOrderRela(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_RELAROOM_QUIT_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*3 参数获取*/
		String relaId = jsonObject.getString("relaId");
		JSONArray arrays = jsonObject.getJSONArray("roomOrderIdList");
        String jsonText = arrays.toJSONString();
        List<String> roomOrderIdList = JSON.parseObject(jsonText, new TypeReference<ArrayList<String>>() {
        });
        RoomOrderModel roomOrderModel = new RoomOrderModel();
		roomOrderModel.setRelaId(relaId);
		List<RoomOrderModel> roomOrderListFind = roomOrderService.list(roomOrderModel);
		if(roomOrderListFind.size() == roomOrderIdList.size()+1){
			for(RoomOrderModel item :roomOrderListFind){
				/* 4 模板替代获取*/
				templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_RELAROOM_QUIT_LOG_BIZ_TYPE.getName());
				templateTY = templateTY.replace("#roomCode#",item.getRoomCode());
				templateTY = templateTY.replace("# operationTime #",Format.getForMatDayForFull(new Date()));
					/* 5 组织日志记录*/
				LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
				logBizRecordModel.setRoomCode(item.getRoomCode());
				logBizRecordModel.setGuestName(item.getName());
				logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
				logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
				logBizRecordModel.setRemark(roomOrderModel.getRemark());
				/*6 保存日志*/
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
			}
		}else{
			for(String id : roomOrderIdList){
				RoomOrderModel item  = roomOrderService.findById(id);
				/* 4 模板替代获取*/
				templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_RELAROOM_QUIT_LOG_BIZ_TYPE.getName());
				templateTY = templateTY.replace("#roomCode#",item.getRoomCode());
				templateTY = templateTY.replace("# operationTime #",Format.getForMatDayForFull(new Date()));
					/* 5 组织日志记录*/
				LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
				logBizRecordModel.setRoomCode(item.getRoomCode());
				logBizRecordModel.setGuestName(item.getName());
				logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
				logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
				logBizRecordModel.setRemark(roomOrderModel.getRemark());
				/*6 保存日志*/
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
			}
		}
	}
	private void roomOrder_joinOrderRela(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
			/*1 从redis中日志类型*/
			LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
			/*2 redis获取业务类型*/
			/*直接入住*/
			LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_RELAROOM_LOG_BIZ_TYPE.getCode());
			String templateTY = modelForTY.getTemplate();
			
			/*参数获取*/
			JSONArray arrays = jsonObject.getJSONArray("roomOrderIdList");
	        String jsonText = arrays.toJSONString();
	        List<String> roomOrderIdList = JSON.parseObject(jsonText, new TypeReference<ArrayList<String>>() {
	        });
			for(String roomOrderId :roomOrderIdList){
				RoomOrderModel roomOrderModel  = roomOrderService.findById(roomOrderId);
				/*模板替代获取*/
				templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_RELAROOM_LOG_BIZ_TYPE.getName());
				templateTY = templateTY.replace("#roomCode#", roomOrderModel.getRoomCode());
					/*组织日志记录*/
				LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
				logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
				logBizRecordModel.setGuestName(roomOrderModel.getName());
				logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
				logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
				logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
				logBizRecordModel.setRemark(roomOrderModel.getRemark());
				/*保存日志*/
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
			}
		
	}
	private void roomOrder_dissolveOrderRela(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_RELAROOM_QUIT_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		
		/*参数获取*/
		List<RoomOrderModel> roomOrderList = (List<RoomOrderModel>) re.getData();
		for(RoomOrderModel roomOrderModel :roomOrderList){
			/*模板替代获取*/
			templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_RELAROOM_QUIT_LOG_BIZ_TYPE.getName());
			templateTY = templateTY.replace("#roomCode#", roomOrderModel.getRoomCode());
			templateTY = templateTY.replace("# operationTime #",Format.getForMatDayForFull(new Date()));
				/*组织日志记录*/
			LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
			logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
			logBizRecordModel.setGuestName(roomOrderModel.getName());
			logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
			logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
			logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
			logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
			logBizRecordModel.setRemark(roomOrderModel.getRemark());
			/*保存日志*/
			this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
		}
	
	}
	/*private void roomOrder_quitOrderRela(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		1 从redis中日志类型
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		2 redis获取业务类型
		直接入住
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_RELAROOM_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		
		参数获取
		String relaId = jsonObject.getString("relaId");
		RoomOrderModel roomOrderModelParam = new RoomOrderModel();
		roomOrderModelParam.setRelaId(relaId);
		List<RoomOrderModel> finds = roomOrderService.list(roomOrderModelParam);
		JSONArray arrays = jsonObject.getJSONArray("roomOrderIdList");
        String jsonText = arrays.toJSONString();
        List<String> roomOrderIdList = JSON.parseObject(jsonText, new TypeReference<ArrayList<String>>() {
        });
		
		if(finds.size() == roomOrderIdList.size()+1){
			roomOrder_dissolveOrderRela(path, jsonObject, re, groupId, hotelId, empId, empName);
		}else{
			for(RoomOrderModel roomOrderModel :finds){
				模板替代获取
				templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_RELAROOM_LOG_BIZ_TYPE.getName());
				templateTY = templateTY.replace("#roomCode#", roomOrderModel.getRoomCode());
				组织日志记录
				LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
				logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
				logBizRecordModel.setGuestName(roomOrderModel.getName());
				logBizRecordModel.setGuestId(null);
				保存日志
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
			}
		}
	}*/
	private void roomOrder_updateSelectTive(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_UPDATE_REMARK_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*3 参数获取*/
		String id = jsonObject.getString("id");
		RoomOrderModel roomOrderModel  = roomOrderService.findById(id);
		/* 4 模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_UPDATE_REMARK_LOG_BIZ_TYPE.getName())
		.replace("#roomCode#",roomOrderModel.getRoomCode())
		.replace("#remark#" ,roomOrderModel.getRemark());
			/* 5 组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
		logBizRecordModel.setRemark(roomOrderModel.getRemark());
		/*6 保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
	}
	private BigDecimal getDayPrice(Date checkinTime,Date checkoutTime,BigDecimal monthPrice ){
		RoomTypeStatusModel roomTypeStatusModel = roomTypeStatusService.getApartPrice
				(checkinTime,checkoutTime,monthPrice);
		return roomTypeStatusModel.getDayPrice();
	}
	private void roomOrder_updateForPrice(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.ORDER_TRANS_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.ORDER_UPDATE_ROOMPRICE_LOG_BIZ_TYPE.getCode());
		String templateTY = modelForTY.getTemplate();
		/*参数获取*/
		BigDecimal roomPrice = jsonObject.getBigDecimal("roomPrice");
		String reasonRemark = jsonObject.getString("reasonRemark");
		BigDecimal monthPrice = jsonObject.getBigDecimal("monthPrice");
		RoomOrderModel roomOrderModel  = (RoomOrderModel) re.getData();
		/*模板替代获取*/
		templateTY = templateTY.replace("#businessTypeName#", LogBizTypeEnum.ORDER_UPDATE_ROOMPRICE_LOG_BIZ_TYPE.getName());
		templateTY = templateTY.replace("#newRoomPrice#", roomPrice!=null?roomPrice.toString():"0");
		templateTY = templateTY.replace("#remark#", reasonRemark);
		templateTY = templateTY.replace("#oldRoomPrice#", roomOrderModel.getRoomPrice().toString());
		templateTY = templateTY.replace("#newRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#oldRoomCode#",roomOrderModel.getRoomCode());
		templateTY = templateTY.replace("#newPriceSchemeName#", roomOrderModel.getPriceSchemeName()!=null?roomOrderModel.getPriceSchemeName():"");
		templateTY = templateTY.replace("#oldPriceSchemeName#", roomOrderModel.getPriceSchemeName()!=null?roomOrderModel.getPriceSchemeName():"");
		if(monthPrice != null){
			RoomTypeStatusModel roomTypeStatusModel = roomTypeStatusService.getApartPrice
					(roomOrderModel.getCheckinTime(),roomOrderModel.getCheckoutTime(),monthPrice);
			templateTY = templateTY.replace("#newRoomPrice#",roomTypeStatusModel.getDayPrice().toString());
		}
			/*组织日志记录*/
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setRoomCode(roomOrderModel.getRoomCode());
		logBizRecordModel.setGuestName(roomOrderModel.getName());
		logBizRecordModel.setGuestId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
		logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
		logBizRecordModel.setOrderNo(roomOrderModel.getRoomOrderNo());
		logBizRecordModel.setRemark(reasonRemark);
		/*保存日志*/
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, templateTY);
	}
	private void roomOrder_edit(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.CHECKIN_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		/*直接入住*/
		LogBizTypeModel modelForTYZ = getlogBizModel(LogBizTypeEnum.CHECKIN_DIRECT_LOG_BIZ_TYPE.getCode());
		String templateZ = modelForTYZ.getTemplate();
		/*预订入住同上不获取了*/
		LogBizTypeModel modelForTYY = getlogBizModel(LogBizTypeEnum.CHECKIN_RESERVE_LOG_BIZ_TYPE.getCode());
		String templateY = modelForTYY.getTemplate();
		/*参数获取*/
		RoomOrderModel roomOrderModel = jsonObject.getObject("roomOrder", RoomOrderModel.class);
		/*JSONArray arrays = jsonObject.getJSONArray("listRoomOrder");
        String jsonText = arrays.toJSONString();
        List<RoomOrderVO> listRoomOrder = JSON.parseObject(jsonText, new TypeReference<ArrayList<RoomOrderVO>>() {
        });*/
		
		List<String> roomirderIdList= (List<String>) re.getData();
		/*模板替代获取*/
        templateY = templateY.replace("#businessTypeName#",LogBizTypeEnum.CHECKIN_RESERVE_LOG_BIZ_TYPE.getName());
        templateZ = templateZ.replace("#businessTypeName#", LogBizTypeEnum.CHECKIN_DIRECT_LOG_BIZ_TYPE.getName());
        String templateForY = getTemplateForRoomOrder(templateY, roomOrderModel);
        String templateForZ = getTemplateForRoomOrder(templateZ, roomOrderModel);
        
		for(String id : roomirderIdList){
			RoomOrderModel item = roomOrderService.findById(id);
			if(item.getRentType() == RentTypeEnum.APRTMENT.getValue()){
				templateForY=templateForY.replace("#roomPrice#", getDayPrice(new Date(), item.getCheckoutTime(), item.getMonthPrice()).toString());
				templateForZ = templateForZ.replace("#roomPrice#", getDayPrice(new Date(), item.getCheckoutTime(), item.getMonthPrice()).toString());
			}else{
				templateForZ = templateForZ.replace("#roomPrice#", item.getRoomPrice().toString());
				templateForY=templateForY.replace("#roomPrice#", item.getRoomPrice().toString());
			}
			templateForY = templateForY.replace("#roomCode#",item.getRoomCode());
			templateForZ = templateForZ.replace("#roomCode#",item.getRoomCode());
			templateForY = templateForY.replace("# channelName #",item.getChannelName());
			templateForZ = templateForZ.replace("# channelName #",item.getChannelName());
			templateForY = templateForY.replace("#checkinStandardName#",item.getCheckinStandardName());
			templateForZ = templateForZ.replace("#checkinStandardName#",item.getCheckinStandardName());
			/*组织日志记录*/
			LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
			logBizRecordModel.setRoomCode(item.getRoomCode());
			logBizRecordModel.setGuestName(item.getName());
			logBizRecordModel.setGuestId(item.getMainGuestId());
			logBizRecordModel.setUpdateUserId(roomOrderModel.getMainGuestId());
			logBizRecordModel.setUpdateUserName(roomOrderModel.getName());
			logBizRecordModel.setOrderNo(item.getRoomOrderNo());
			logBizRecordModel.setRemark(item.getRemark());
			/*保存日志*/
			if(item.getReserveId()!=null){
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTYY, groupId, hotelId, empId, empName, templateForY);
			}else{
				this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTYZ, groupId, hotelId, empId, empName, templateForZ);
			}
		}
	}
	private void reserve_edit(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.RESERVE_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY =getlogBizModel(LogBizTypeEnum.RESERVE_LOG_BIZ_TYPE.getCode());
		String template = modelForTY.getTemplate();
		String id = re.getData().toString();
		ReserveModel reserveModel = reserverService.findById(id);
		JSONArray arrays = jsonObject.getJSONArray("listReserveRoom");
        String jsonText = arrays.toJSONString();
        List<ReserveVO> listReserveRoomVO = JSON.parseObject(jsonText, 
        		new TypeReference<ArrayList<ReserveVO>>(){});
        List<String> roomCodeList = new ArrayList<>();
        for(ReserveVO res:listReserveRoomVO){
        	roomCodeList.add(res.getRoomCode());
        }
        template = template.replace("#bussinessTypeName#", LogBizTypeEnum.RESERVE_LOG_BIZ_TYPE.getName());
		template = this.getTemplateForReserve(template, reserveModel);
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		logBizRecordModel.setGuestName(reserveModel.getReserveName());
		logBizRecordModel.setOrderNo(reserveModel.getReserveNo());
		logBizRecordModel.setRoomCode(roomCodeList.toString());
		logBizRecordModel.setGuestId(null);
		logBizRecordModel.setRemark(reserveModel.getRemark());
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, template);
	}
	private void reserve_cancelReserve(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT = getlogBizModel(LogBizTypeEnum.RESERVE_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.RESERVE_CANCEL_LOG_BIZ_TYPE.getCode());
		String template = modelForTY.getTemplate();
		String id = jsonObject.getString("id");
		ReserveModel reserveModel = reserverService.findById(id);
		ReserveRoomModel reserveRoomModel = new ReserveRoomModel();
		reserveRoomModel.setReserveId(reserveModel.getId());
        List<ReserveRoomModel> listReserveRoom =  reserveRoomService.list(reserveRoomModel);
        List<String> roomCodeList = new ArrayList<>();
        for(ReserveRoomModel res:listReserveRoom){
        	if(res.getRoomCode()!=null){
        		roomCodeList.add(res.getRoomCode());
        	}
        }
        template = template.replace("#bussinessTypeName#", LogBizTypeEnum.RESERVE_CANCEL_LOG_BIZ_TYPE.getName());
		template = this.getTemplateForReserve(template, reserveModel);
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		
		logBizRecordModel.setGuestName(reserveModel.getReserveName());
		logBizRecordModel.setRoomCode(roomCodeList.toString());
		logBizRecordModel.setGuestId(null);
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, template);
	}
	private void reserve_recoverReserve(String path, JSONObject jsonObject, Result re,String groupId,String hotelId,String empId,String empName){
		/*1 从redis中日志类型*/
		LogBizTypeModel modelForT =getlogBizModel(LogBizTypeEnum.RESERVE_LOG_TYPE.getCode());
		/*2 redis获取业务类型*/
		LogBizTypeModel modelForTY = getlogBizModel(LogBizTypeEnum.RESERVE_RECOVER_LOG_BIZ_TYPE.getCode());
		String template = modelForTY.getTemplate();
		String id = jsonObject.getString("id");
		ReserveModel reserveModel = reserverService.findById(id);
		ReserveRoomModel reserveRoomModel = new ReserveRoomModel();
		reserveRoomModel.setReserveId(reserveModel.getId());
        List<ReserveRoomModel> listReserveRoom =  reserveRoomService.list(reserveRoomModel);
        List<String> roomCodeList = new ArrayList<>();
        for(ReserveRoomModel res:listReserveRoom){
        	if(res.getRoomCode()!=null){
        		roomCodeList.add(res.getRoomCode());
        	}
        }
        template = template.replace("#bussinessTypeName#", LogBizTypeEnum.RESERVE_RECOVER_LOG_BIZ_TYPE.getName());
		template = this.getTemplateForReserve(template, reserveModel);
		LogBizRecordModel logBizRecordModel = new LogBizRecordModel();
		
		logBizRecordModel.setGuestName(reserveModel.getReserveName());
		logBizRecordModel.setRoomCode(roomCodeList.toString());
		logBizRecordModel.setGuestId(null);
		this.saveLogBizRevord(logBizRecordModel, modelForT, modelForTY, groupId, hotelId, empId, empName, template);
	}
	/*1 从redis中日志类型*/
	private LogBizTypeModel getlogBizModel(String enumCode){
		String logBizForT = (String) cache.get
		(enumCode + CacheKey.KEY_CACHE_LOG_BIZ_TYPE);
			JSONObject jsonForT = JSONObject.parseObject(logBizForT);
			LogBizTypeModel modelForT = JSONObject.toJavaObject(jsonForT, LogBizTypeModel.class);
			return modelForT;
	}
	/**
	 * 
	 * 功能：<br/>
	 * LogBizRecordModel 日至对象
	 * modelForT 日志类型对象
	 * modelForTY 日志业务类型对象
	 * template 模板
	 * @author sunyl
	 * @Date 2018年1月26日 上午10:45:25 <br/>
	 */
	private void saveLogBizRevord(LogBizRecordModel logBizRecordModel,LogBizTypeModel modelForT,LogBizTypeModel modelForTY,
			String groupId,String hotelId,String empId,String empName,String template){
		logBizRecordModel.setLogBizTypeId(modelForT.getId());
		logBizRecordModel.setLogBizTypeName(modelForT.getName());
		
		logBizRecordModel.setBizTypeId(new Byte(modelForTY.getId()));//??
		logBizRecordModel.setBizTypeCode(modelForTY.getCode());
		logBizRecordModel.setBizTypeName(modelForTY.getName());
		logBizRecordModel.setGroupId(groupId);
		logBizRecordModel.setHotelId(hotelId);
		logBizRecordModel.setBizDetail(template);
		logBizRecordModel.setCreateUserId(empId);
		logBizRecordModel.setCreateUserName(empName);
		logBizRecordModel.setUpdateUserId(empId);
		logBizRecordModel.setUpdateUserName(empName);
		logBizRecordService.save(logBizRecordModel);
	}
	private String getTemplateForReserve(String template,ReserveModel reserveModel){
		template = template.replace("#orderNo#",reserveModel.getReserveNo()).
				replace("#orderDate#",Format.getForMatDayForFull(reserveModel.getInTime()))
        .replace("#customTypeName#", reserveModel.getCustomTypeName())
        .replace("#checkinTypeName#",CheckinTypeEnum.getNameByValue(reserveModel.getCheckinType()))
        .replace("#channelName#", ChannelEnum.getNameByValue(reserveModel.getChannel()))
        .replace("#checkinStandardName#", reserveModel.getCheckinStandardName())
        .replace("#dayCount#", reserveModel.getDayCount().toString())
        .replace("#keepTime#", Format.getForMatDayForFull(reserveModel.getKeepTime()));
        return template;
	}
	private String getTemplateForRoomOrder(String template,RoomOrderModel model){
		template = template.replace("#checkinTime#",Format.getForMatDayForFull(new Date()))
		.replace("#checkoutTime#", Format.getForMatDayForFull(model.getCheckoutTime()))
		.replace("#dayCount#", String.valueOf(Math.round((model.getCheckoutTime().getTime()- new Date().getTime())/1000/60/60/24)))
		.replace("#customTypeName#", model.getCustomTypeName())
		.replace("#channelName #", ChannelEnum.getNameByValue(model.getChannel()))
		.replace("#checkinStandardName #", model.getCheckinStandardName());
		return template;
	}
}
